﻿﻿<?php
class LoginAction extends BaseApiAction{

	private $gw_address = null;
	private $gw_port    = null;
	private $gw_id  = null;
	private $mac    = null;
	private $url    = null;
	private $logout = null;

    /**
     * 认证跳转页面
     */
	public function index(){

        if (isset($_REQUEST["gw_address"])) {
            $this->gw_address = $_REQUEST['gw_address'];
            cookie('gw_address', $_REQUEST['gw_address']);
        }

        if (isset($_REQUEST["gw_port"])) {
            $this->gw_port = $_REQUEST['gw_port'];
            cookie('gw_port', $_REQUEST['gw_port']);
        }

        if (isset($_REQUEST["gw_id"])) {
            $this->gw_id = $_REQUEST['gw_id'];
            cookie('gw_id', $_REQUEST['gw_id']);
        }

        if (isset($_REQUEST["url"])) {
            $this->url = $_REQUEST['url'];
            cookie('gw_url', $_REQUEST['url']);
        }

        if (isset($_REQUEST["mac"])) {
            cookie('mac', $_REQUEST['mac']);
            $mac = $_REQUEST['mac'];
        }

        if (isset($_REQUEST["state"])) {
            cookie('state', $_REQUEST['state']);
        }

        $sql  = "select a.*,b.shopname,b.authmode,b.maxcount,b.linkflag,b.sh,b.eh,b.pid,b.countflag,b.countmax,b.tpl_path from " . C('DB_PREFIX') . "routemap a left join " . C('DB_PREFIX') . "shop b on a.shopid=b.id ";
        $sql .= " where a.gw_id='" . $this->gw_id . "' limit 1";
        $db   = D('Routemap');
        $addb = D('Ad');
        $info = $db->query($sql);
        $nowdate = getNowDate();//当前日期

        if(cookie('state') == 0){
            $str = 1;
        }else{
            $str = 0;
        }
        if($str){
            //广告位置在认证前页面
//            $ad = M('pushadv')->where(array('aid'=>1))->field('id,pic,aid')->limit(3)->select();
//            $this->assign('ad', $ad);
//            $this->display('pad');
            //添加广告
            $where['uid'] = $info[0]['shopid'];
            $where['ad_pos'] = 2;
            $ad = $addb->where($where)->field('id,ad_thumb,mode')->select();
            $ids = "";
            foreach ($ad as $k => $v) {
                $ids .= $v['id'] . ",";
            }
            $this->assign('ad', $ad);
            $this->assign('adid', $ids);
            $this->display('pad');
        }else{
            if (!empty($this->gw_id)){
                if($info != false){
                    $show = 1;
                    $max = $info[0]['maxcount'];
                    $limit = $info[0]['linkflag'];
                    if ($limit == 0) {
                        $where['shopid'] = $info[0]['shopid'];
                        $count = D('member')->where($where)->count();
                        if ($count > $max) {
                            $show = 0;
                        }
                    }
                    cookie('shopid', $info[0]['shopid']);

                    $authmode = $info[0]['authmode'];
                    $where['uid'] = $info[0]['shopid'];
                    $where['ad_pos'] = 0;
                    $ad = $addb->where($where)->field('id,ad_thumb,mode')->select();
                    $ids = "";
                    foreach ($ad as $k => $v) {
                        $ids .= $v['id'] . ",";
                    }
                    /*判断是否在允许上网时段*/
                    $hour = (int)date("H");
                    if (!empty($info[0]['sh']) && !empty($info[0]['eh']) && $info[0]['sh'] != "" && $info[0]['eh'] != "") {
                        $sh = (int)$info[0]['sh']; $eh = (int)$info[0]['eh'];
                        $auth['opensh'] = $sh; $auth['openeh'] = $eh;
                        $auth['openflag'] = true;//设置时段
                        if ($hour >= $sh && $hour <= $eh) {
                            $auth['open'] = true;
                        } else {
                            $auth['open'] = false;
                        }
                    } else {
                        $auth['open'] = true;
                        $auth['openflag'] = false;//未设置
                    }
                    /*认证方式*/
                    if ($authmode == null || $authmode == "") {
                        $auth['reg'] = 1;
                    } else {
                        $tmp = explode('#', $authmode);
                        foreach ($tmp as $v) {
                            if ($v != '#' && $v != '') {
                                $arr[] = $v;
                            }
                        }
                        foreach ($arr as $v) {
                            $temp = explode('=', $v);
                            if (count($temp) > 1 && $temp[0] == '3') {
                                $auth['wx'] = 1;
                            } else {
                                if ($v == '0') {
                                    $auth['reg'] = 1;
                                }
                                if ($v == '1') {
                                    $auth['phone'] = 1;
                                }
                                if ($v == '2') {
                                    $auth['allow'] = 1;
                                }
                                if ($v == '4') {
                                    $auth['qq'] = 1;
                                }
                            }
                        }
                    }

                    /*判断是否启用认证限制*/
                    if ($info[0]['countflag'] > 0) {
                        $maxcount = $info[0]['countmax'];
                        $authdb = D('Authlist');
                        $countwhere['mac']      = $mac;
                        $countwhere['shopid']   = $info[0]['shopid'];
                        $countwhere['add_date'] = $nowdate;
                        $auth_count = $authdb->where($countwhere)->count();

                        if (($maxcount - $auth_count) <= 0) {
                            $auth['overmax'] = 1;
                        } else {
                            $auth['overmax'] = 0;
                        }
                    } else {
                        $auth['overmax'] = 0;
                    }

                    $this->assign('ad', $ad);
                    $this->assign('adid', $ids);
                    $this->assign('show', $show);
                    $this->assign("authmode", $auth);
                    $this->assign("shopinfo", $info);

                    //模板选择
                    $tplkey = $info[0]['tpl_path'];
                    switch($tplkey){
                        case "wifiadv":
                            $this->display('index$' . $tplkey);
                            break;
                        case "default3":
                            $this->display($tplkey);
                            break;
                        case "default4":
                            $this->display($tplkey);
                            break;
                        default:
                            $this->display();
                    }
                }else{
                    $this->assign('gw_id',$_COOKIE['gw_id']);
                    $this->display("gw");
                }
            }else{
                echo '参数不正确2';
            }
        }
	}

    /**
     *统计
     */
	public function countad(){
		$gid=cookie('gw_id');
		$sid=cookie('shopid');
		if(empty($gid)||empty($sid)){			
			exit;
		}
		
		$ids   = I('post.ids');
		$idarr = explode(',', $ids);
		/*统计展示*/
        $tr   = new Model();

        $time = time();
        $tr->startTrans();

        $arrdata['showup']  = 1;
        $arrdata['hit']     = 0;
        $arrdata['shopid']  = $sid;
        $arrdata['add_time']= $time;
        $arrdata['add_date']= getNowDate();
        $arrdata['mode']    = 1;

        foreach($idarr as $v){
            if($v!=""){
                $arrdata['aid'] = $v;
                $tr->table(C('DB_PREFIX')."adcount")->add($arrdata);
            }
        }
        $tr->commit();
	}
}